* Project      :  The Full, Persistent, and Symmetric Pass-Through of a Temporary VAT Cut
* Authors      :  Márcia Silva-Pereira, João Quelhas, Tiago Bernardino, Ricardo Duque Gabriel
* Date         :  01/04/2025
* Description  :  Event-study Regression - Persistency
* Dependencies :
* Modifications: (add date, author and change)

*********************************************************
*                    Persistency                        *
*********************************************************

local vce_cluster ${vce_cluster}
local weights ${weights}
local variable ${variable}
local aggregation ${aggregation}

// Load the cleaned dataset.
use "${path_work}/auxiliar/imputation_0_only_food.dta", clear

collapse (${aggregation}) ${variable} (first) ecoicop5 treatment weight, by (id_sm five_day_number)

// Compute a price index with base in the day before policy announcement I.
local   base_date 22
egen    base_price_23032023 = total(${variable}/ (five_day_number == `base_date')), by(id_sm)
gen     index_23032023 = (${variable}/ base_price_23032023) * 100

xtset id_sm five_day_number

*********************************************************

* FIGURE 5: Pass-Through Persistency of the 2023 Temporary VAT Cut in Portugal
*********************************************************

preserve

// Keep the data around the event.
keep if five_day_number >= 17
keep if five_day_number <= 66

// Policy variable: 1 after the reference date.
replace treatment = 0 if five_day_number < 23

if ${weights} == 0 {

// Run the event-study regression.
xtevent index_23032023, policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} 41) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe diffavg
	
}

if ${weights} == 1 {

// Run the event-study regression.
xtevent index_23032023 [pweight = weight], policyvar(treatment) panelvar(id_sm) timevar(five_day_number) window(${window_low} 41) vce(cluster ${vce_cluster}) impute(nuchange) reghdfe diffavg
		
}

matrix results = r(delta)
matrix vcv = r(Vdelta)
local z = 1.96

file open  myfile using "${results_figures}/temp_file.txt", write text replace
file write myfile "lower_ci, coefficient, upper_ci" _n _n

forvalues i = 10/47 {
	
	scalar coef = results[1,`i']
	scalar se   = sqrt(vcv[`i',`i'])
	generate coeff = coef
	generate z = `z'
	generate see = se
	scalar lb = coeff - z * see
	generate lbb = lb
	scalar ub = coeff + z * see
	generate ubb = ub
	format %12.0g coeff lbb ubb
	file write myfile (lbb) ", " (coeff) ", " (ubb) _n _n
	drop coeff z see lbb ubb

	}

file close myfile

restore

import delimited "${results_figures}/temp_file.txt", numericcols(_all) clear

gen line_bar = -5.66
gen five_day_period = _n + 27

twoway (rcap lower_ci upper_ci five_day_period, lcolor(gs11) lwidth(medthin)) (line coefficient five_day_period, fcolor(dknavy) lcolor(dknavy) lwidth(medthick) recast(connected)  mcolor(dknavy) msymbol(circle)) (scatteri -5.66 28 -5.66 65,  lcolor("dknavy") lpattern(dash) lwidth(medthin) recast(line)), ytitle("Percentage change") yscale(noline) ylabel(-2(2)-10) xtitle("") xscale(noline) legend(off) xlabel(31 "May-2023" 37 "Jun-2023" 43 "Jul-2023" 49 "Aug-2023" 55 "Sep-2023" 61 "Oct-2023", labsize(medsmall) angle(forty_five) format(%tdMon-CCYY)) text(-6.3 31 "100% Pass-Through", color("dknavy") placement(e)) 

// Export the combined graph as a png file.
graph export "${results_figures}/figure_5.png", as(png) replace width(2400) height(1372)

erase "${results_figures}/temp_file.txt"

*********************************************************


* FIGURE A.7: Distribution of the Cumulative Percentage Change for Treated Items
*********************************************************

// Load the cleaned dataset.
use "${path_work}/auxiliar/imputation_0_only_food.dta", clear

// Filter the data for specific dates.
keep if date == td(23/03/2023) | date == td(23/05/2023) | date == td(23/07/2023) | date == td(23/09/2023) | date == td(23/12/2023)

// Compute a price index with base in the day before policy announcement I.
egen price_less_1m = total(price / (date == td(23/03/2023))), by(id_sm)
gen price_more_1m = ((price / price_less_1m) - 1)*100 if date == td(23/05/2023)
gen price_more_3m = ((price / price_less_1m) - 1)*100 if date == td(23/07/2023)
gen price_more_6m = ((price / price_less_1m) - 1)*100 if date == td(23/09/2023)
gen price_more_9m = ((price / price_less_1m) - 1)*100 if date == td(23/12/2023)

// Create individual histograms and save them with unique names.
histogram price_more_1m if treatment == 1 & price_more_1m >= -10 & price_more_1m <= 10, width(1) ///
   percent yscale(noline) ylabel(0(20)100) ///
   xtitle("Percentage change", size(medsmall)) xscale(noline) xlabel(-10(1)10, labels) ///
   fcolor("dknavy") lcolor("dknavy") lwidth(none none none none) title("1 Month After Implementation") ///
   ytitle("Percentage of items", size(medsmall)) plotregion(lcolor(black)) name(hist1, replace)

histogram price_more_3m if treatment == 1 & price_more_3m >= -10 & price_more_3m <= 10, width(1) ///
   percent yscale(noline) ylabel(0(20)100) ///
   xtitle("Percentage change", size(medsmall)) xscale(noline) xlabel(-10(1)10, labels) ///
   fcolor("dknavy") lcolor("dknavy") lwidth(none none none none) title("3 Months After Implementation") ///
   ytitle("Percentage of items", size(medsmall)) plotregion(lcolor(black)) name(hist2, replace)

histogram price_more_6m if treatment == 1 & price_more_6m >= -10 & price_more_6m <= 10, width(1) ///
   percent yscale(noline) ylabel(0(20)100) ///
   xtitle("Percentage change", size(medsmall)) xscale(noline) xlabel(-10(1)10, labels) ///
   fcolor("dknavy") lcolor("dknavy") lwidth(none none none none) title("6 Months After Implementation") ///
   ytitle("Percentage of items", size(medsmall)) plotregion(lcolor(black)) name(hist3, replace)

grstyle init
grstyle set plain, nogrid box noextend
grstyle graphsize x 8
grstyle graphsize y 14
grstyle color background white

// Combine the histograms into one plot.
graph combine hist1 hist2 hist3, col(1)

// Export the combined graph as a png file.
graph export "${results_figures}/figure_a6.png", as(png) replace width(2400) height(1372)

graph close _all

grstyle init
grstyle set plain, nogrid box noextend
grstyle graphsize x 14
grstyle graphsize y 8
grstyle color background white

*********************************************************
